package edu.nf.waimai.service.rider.impl;

import edu.nf.waimai.model.ridertakeorders.MerchantOrders;
import edu.nf.waimai.model.ridertakeorders.UserInformation;
import edu.nf.waimai.service.rider.RiderAddressService;
import edu.nf.util.DBUtils;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanListHandler;

import java.util.List;

/**
 * @Date 2023-06-21
 * @Author 陈
 */
public class RiderAddressServiceImpl implements RiderAddressService {
    @Override
    public List<MerchantOrders> MerchantAddress() {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "SELECT o.order_id, o.u_id, o.rider_id, o.orders_status, o.orders_type, o.orders_details,s.shop_id, s.shop_name, s.shop_phone, s.shop_address " +
                "FROM orders o " +
                "LEFT JOIN shop s ON o.shop_id = s.shop_id " +
                "WHERE o.orders_status = 1";
        BeanListHandler<MerchantOrders> handler = new BeanListHandler<>(MerchantOrders.class);
        return executor.executeQuery(sql,handler);
    }

    @Override
    public List<UserInformation> UserInfoAddress(Integer id) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "SELECT o.order_id, o.u_id, o.shop_id, o.rider_id, o.orders_status, a.address_name, a.address_phone, a.detail " +
                "FROM orders o " +
                "JOIN address a ON o.u_id = a.u_id " +
                "WHERE o.u_id = ?";
        BeanListHandler<UserInformation> handler = new BeanListHandler<>(UserInformation.class);
        return executor.executeQuery(sql,handler,id);
    }

    @Override
    public Integer OrdersRidUpdate(Integer rid, Integer id) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        System.out.println();
        String sql = "update orders set rider_id=? where order_id=?";
        return executor.executeUpdate(sql,rid,id);
    }

    @Override
    public Integer OrdersStaUpdate(Integer sta, Integer rid) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "update orders set orders_status=? where rider_id=?";
        return executor.executeUpdate(sql, sta, rid);
    }
}
